package com.jandan.ui.client;

import java.util.TreeMap;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.layout.FitLayout;
import com.jandan.ui.client.service.LessonService;
import com.jandan.ui.client.service.LessonServiceAsync;
import com.jandan.ui.client.util.ClientUtil;

public class ChooseLessonWindow extends Window {
	private TreeMap<Long, String> lessons;
	private ILessonOperator lessonOperator;
	private int flag;

	public ChooseLessonWindow() {

	}

	public ChooseLessonWindow(ILessonOperator lessonOperator, int flag) {
		// TODO Auto-generated constructor stub
		this.lessonOperator = lessonOperator;
		this.flag = flag;

		lessons = lessonOperator.getLessons();

		createComponent();
		if (this.flag == ClientUtil.DELETE_LESSON)
			this.setTitle("删除课程");

	}

	private void createComponent() {
		this.setModal(true);
		this.setCloseAction(Window.CLOSE);

		this.setWidth(290);
		this.setHeight(148);

		this.setResizable(false);
		this.setLayout(new FitLayout());
		this.setPaddings(5);

		final FormPanel formPanel = new FormPanel();
		formPanel.setFrame(true);
		formPanel.setPaddings(5, 5, 5, 0);
		formPanel.setWidth(280);
		formPanel.setLabelWidth(55);
		formPanel.setLabelAlign(Position.RIGHT);

		final LessonComboBox lessonComboBox = new LessonComboBox();
		formPanel.add(lessonComboBox);
		formPanel.setButtonAlign(Position.RIGHT);

		Button ok = new Button("确定");
		Button cancel = new Button("取消");

		ok.addListener(new ButtonListenerAdapter() {
			@Override
			public void onClick(Button button, EventObject e) {
				// TODO Auto-generated method stub
				// 选取id，删除id为此的树节点，删除此课程
				if (formPanel.getForm().isValid()) {
					final long lessonID = Long.parseLong(lessonComboBox.getValue());
					
					String lessonName = lessons.get(lessonID);
					MessageBox.confirm("提示", "确定删除课程" + lessonName + "  " + lessonID,
							new MessageBox.ConfirmCallback() {

								public void execute(String btnID) {
									// TODO Auto-generated method stub
									// MessageBox.alert(lessonID);
									LessonServiceAsync lessonService = LessonService.Util
											.getInstance();
									lessonService.deleteLesson(lessonID,
											new AsyncCallback() {
												public void onFailure(
														Throwable caught) {
													// TODO Auto-generated
													// method stub
													MessageBox.alert("删除课程错误");
												}

												public void onSuccess(
														Object result) {
													// TODO Auto-generated
													// method stub
													lessonOperator
															.deleteLesson(lessonID);
												}
											});
									closeTheWindow();
								}
							});
				}
			}
		});

		cancel.addListener(new ButtonListenerAdapter() {

			@Override
			public void onClick(Button button, EventObject e) {
				// TODO Auto-generated method stub
				// super.onClick(button, e);
				closeTheWindow();
			}

		});

		formPanel.addButton(ok);
		formPanel.addButton(cancel);

		this.add(formPanel);
	}

	private class LessonComboBox extends ComboBox {

		public LessonComboBox() {
			createComponent();
		}

		private void createComponent() {
			final Store store = new SimpleStore(new String[] { "id", "name" },
					ClientUtil.mapToStringArray(lessons));
			store.load();
			this.setForceSelection(true);
			this.setFieldLabel("选择课程");
			this.setMinChars(1);
			// this.setFieldLabel("");
			this.setStore(store);
			this.setDisplayField("name");
			this.setMode(ComboBox.LOCAL);
			this.setTriggerAction(ComboBox.ALL);
			this.setValueField("id");
			this.setEmptyText("选择要删除的课程");

			this.setAllowBlank(false);
			//	
			// this.setLoadingText("Searching...");
			this.setTypeAhead(true);
			this.setSelectOnFocus(true);
			//	

			// this.setEmptyText("请选择课程");
			this.setWidth(150);
			this.setTriggerAction(ComboBox.ALL);
			this.setEditable(false);

			this.setHideTrigger(false);
		}
	}

	private void closeTheWindow() {
		this.close();
	}
}
